package com.mz.demo.service.impl;

import com.mz.demo.dao.PermissionMapper;
import com.mz.demo.entity.SysPermission;
import com.mz.demo.entity.vo.PermissionVo;
import com.mz.demo.service.PermissionService;
import com.mz.demo.utils.entity.GlobalException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Transactional
    @Override
    public void removePermission(Long permissionId) {
        permissionMapper.deleteById(permissionId);
    }

    @Transactional
    @Override
    public void updatePermissionInfo(PermissionVo permissionVo) {
        SysPermission sysPermission = permissionVoToSysPermission(permissionVo);
        permissionMapper.updateById( sysPermission );
        return;
    }

    @Override
    public List<SysPermission> getPermissionTree() {

        //获取到所有权限
        List<SysPermission> list =  permissionMapper.selectList(null);
        List<SysPermission> firstList = list.stream().filter(item->item.getLevel() == 1).map( item->{
            List<SysPermission> children = getPermissionTree( item, list );
            item.setChildren(children);
            return item;
        }).collect(Collectors.toList());

        return firstList;
    }


    private List<SysPermission> getPermissionTree( SysPermission curPerm, List<SysPermission> tree ){
       return tree.stream().filter( item->item.getLevel() == curPerm.getLevel()+1 && item.getParentId().equals( curPerm.getId() ) )
                .map( item->{
                    List<SysPermission> children = getPermissionTree( item, tree );
                    item.setChildren(children);
                    return item;
                }).collect(Collectors.toList());
    }

    @Transactional
    @Override
    public void addPermission(PermissionVo permissionVo) {

        permissionVo.setId(null);
        SysPermission sysPermission = permissionVoToSysPermission(permissionVo);
        if( sysPermission.getParentId() == 0 ){
            sysPermission.setLevel(1);
            permissionMapper.insert( sysPermission );
            return;
        }

        SysPermission parentPerm = permissionMapper.selectById(sysPermission.getParentId());
        if( parentPerm == null ){
            throw GlobalException.gen(10003, "parentId设置异常，不存在此权限");
        }
        sysPermission.setLevel(parentPerm.getLevel()+1);
        permissionMapper.insert( sysPermission );

        return;
    }


    private SysPermission permissionVoToSysPermission( PermissionVo permissionVo ){

        SysPermission sysPermission = new SysPermission();
        sysPermission.setId(permissionVo.getId())
                .setPermissionName(permissionVo.getPermissionName())
                .setPermissionCode(permissionVo.getPermissionCode())
                .setParentId(permissionVo.getParentId())
                .setType(permissionVo.getType());
        return sysPermission;
    }
}
